<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	class Gallery extends CI_Controller
	{	
		private $m_dataObj = null;
		
		public function __construct()
		{
            parent::__construct();
            
            $this->m_dataObj = new DataObject();
		}
		
		
		public function index()
		{	
			// Runs the upload function if user attempts upload, otherwhise display regular view
			
			$this->m_dataObj->SetContent(getGalleryViewUrl());
			
			if ($this->input->post('submitUpload')) 
		    {
		        $this->upload();
		    }
		    else 
		    {
				$this->m_dataObj->SetTitle(getGalleryViewTitle());
				$this->show();
		    }
	   }
	   
	   // Displays the gallery
	   public function show()
	   {
	   		if($this->login_model->IsLoggedIn())
			{
				$this->m_dataObj->SetUser($this->login_model->GetUser());
			}
	   	
	   		$this->m_dataObj->SetContent(getGalleryViewUrl()); 
	   		$this->m_dataObj->SetInformation($this->gallery_model->get_all_images());
	   		   
			$this->load->view(index_page(), array($this->m_dataObj->GetName() => $this->m_dataObj));
	   }
		
		public function upload()
		{		
			// Configuration for upload, validation
			// Config used for upload preferences re-located to config directory
			$this->load->library('upload');
			
			// If upload doesnt succeed, print error
			// Else upload the image, save to database and display the gallery
			if(!$this->upload->do_upload())
			{
				$this->m_dataObj->SetTitle(getGalleryViewFailTitle());
				$this->m_dataObj->SetError($this->upload->display_errors());
				$this->show();
			}
			else
			{
				$this->gallery_model->save_image_path($this->upload->data());
				$this->m_dataObj->SetTitle(getGalleryViewSuccessTitle());
				$this->show();
			} 
		}
		
	}